package com.abc.tacos.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Kar
 * @create 2022-04-20 上午10:42
 */
// @Configuration
// @EnableWebSecurity
public class SecurityConfig_FormLogin extends WebSecurityConfigurerAdapter {

    // 2、使用表单验证
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests() //允许基于HttpServletRequest使用限制访问
                .antMatchers("/Asuka","/design","/order")
                    //.authenticated() // 其他路径必须验证身份
                    .access("hasRole('USER') || hasRole('MANAGERS')") // 验证角色信息
                //.antMatchers("/", "/home")
                //    .access("permitAll") //不需要身份认证
                .and()
                    .formLogin() // 使用表单登陆
                        // .loginPage("/login") // 自定义登陆页面 -> 先到视图控制器（WebConfig）
                        //.permitAll()
                        // .defaultSuccessUrl("/",true)
                .and()
                    .logout()   // 拦截"/logout" post 请求, 清除session数据
                        .logoutSuccessUrl("/")

                // Make H2-Console non-secured; for debug purposes
                // tag::csrfIgnore[]
                .and()
                    .csrf()
                        .ignoringAntMatchers("/h2-console/**")
                // end::csrfIgnore[]

                // Allow pages to be loaded in frames from the same origin; needed for H2-Console
                // tag::frameOptionsSameOrigin[]
                .and()
                    .headers()
                        .frameOptions()
                            .sameOrigin();
                // end::frameOptionsSameOrigin[]
    }
}
